package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EnumUtil;
import com.ruoyi.system.domain.Approval;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.enums.AuditStateEnum;
import com.ruoyi.system.enums.DocTypeEnum;
import com.ruoyi.system.service.IApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PaperMapper;
import com.ruoyi.system.domain.Paper;
import com.ruoyi.system.service.IPaperService;
import com.ruoyi.common.core.text.Convert;

/**
 * 论文Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-04-12
 */
@Service
public class PaperServiceImpl implements IPaperService 
{
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private IApprovalService approvalService;

    /**
     * 查询论文
     * 
     * @param id 论文ID
     * @return 论文
     */
    @Override
    public Paper selectPaperById(Long id)
    {
        return paperMapper.selectPaperById(id);
    }

    /**
     * 查询论文列表
     * 
     * @param paper 论文
     * @return 论文
     */
    @Override
    public List<Paper> selectPaperList(Paper paper)
    {
        return paperMapper.selectPaperList(paper);
    }

    /**
     * 新增论文
     * 
     * @param paper 论文
     * @return 结果
     */
    @Override
    public int insertPaper(Paper paper)
    {
        return paperMapper.insertPaper(paper);
    }

    /**
     * 修改论文
     * 
     * @param paper 论文
     * @return 结果
     */
    @Override
    public int updatePaper(Paper paper)
    {
        return paperMapper.updatePaper(paper);
    }

    /**
     * 删除论文对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaperByIds(String ids)
    {
        return paperMapper.deletePaperByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除论文信息
     * 
     * @param id 论文ID
     * @return 结果
     */
    @Override
    public int deletePaperById(Long id)
    {
        return paperMapper.deletePaperById(id);
    }

    @Override
    public void applyAutdit(Paper writings, SysUser sysUser) {
        writings.setAuditState(AuditStateEnum.AUDIT_ING.getValue());
        updatePaper(writings);
        Approval approval = new Approval();
        approval.setApplyManId(sysUser.getUserId());
        approval.setApplyMan(sysUser.getUserName());
        approval.setApplyManDept(writings.getDepartmentId());
        Date now = new Date();
        approval.setApplyTime(now);
        StringBuilder sb = new StringBuilder();
        String docType = EnumUtil.getIEnum(DocTypeEnum.class,writings.getDocType()).getDesp();
        sb.append(sysUser.getUserName())
                .append(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm",now))
                .append("【")
                .append(writings.getPaperNature())
                .append("<")
                .append(docType)
                .append("申办")
                .append(">")
                .append("】")
                .append("的申请");
        approval.setApprovalName(sb.toString());
        approval.setDocType(writings.getDocType());
        approval.setDocRelationId(writings.getId());
        approval.setApprovalResult(AuditStateEnum.NOT_AUDIT.getValue());
        approvalService.insertApproval(approval);
    }
}
